$(function() {
	ip = location.host;
	//获取LKJ选择的软件组合
	changeObjs = JSON.parse(window.localStorage.getItem('changeObjs'));
	versionChangeWebSocket = null;
	loadPlatFormList();
	softIds = [];
	getVersionTypes(0);
	lkjPlatFormId='';
});
//初始化右侧版本换装内容
function getVersionTypes(type){
	$("#content").empty();
	if(type == 1){//重置功能
		changeObjs = [];
		window.localStorage.removeItem("changeObjs");
	}
	softIds = [];
	var content=[{ID:"1",SYS_NAME:"LKJ软件版本"}];
	var cellContent = [{name:'DMI单元'},{name:'主机单元'},{name:'扩展单元'}];
	for(var i=0;i<content.length;i++){
		for(var j = 0;j<cellContent.length;j++){
			str ="";
			str+='<div class="module" id='+content[i].ID+'><h4>';
			str+=cellContent[j].name;
			str+='</h4>';
			str+=getSelectValue(getVersionTypesName(content[i].ID),j,type);
			$("#content").append(str);
		}
	}
	 $('.multipleSelect').multiselect( { enableClickableOptGroups: true,
         enableCollapsibleOptGroups: true,
         includeSelectAllOption: true});
	 $("#changeBtn").prop("disabled", false);
}
//根据父节点ID获取当前软件版本下都有哪些类型的软件
function getVersionTypesName(sId){
	var content=null;
	$.ajax({
		url : "../../../LKJSoftwareVersion/getTypeName",
		type : "post",
		data : {
			"sId" : sId,
			"type" : ""
		},
		dataType : "json",
		async:false,
		success : function(data) {
			if(data.flag==0){
				content = data.content;
			}else{
				content="";
			}
		},
		error : function() {
		}
	});
	//重置获取的软件数据的顺序，为了排版美观
	resetContent(content);
	return content;
}

//重置获取的软件数据的顺序，为了排版美观
function resetContent(content)
{
	//把揭示数据添加到主机单元中
	content.splice(17,0,content[23]);
	//删除多余的揭示数据
	content.splice(24,1);
	//功能扩展和主控位置替换
	content.splice(18, 1,content.splice(21, 1, content[18])[0]);
}
//将查询到的软件类型进行组装展示各个标签 [{name:'DMI单元'},{name:'主机单元'},{name:'扩展单元'}]
function getSelectValue(content,index,stateType){
	var str="";
	if(content!=""&&content!=null){
		//3个单元的个数索引
		var start = 0;
		var end = 0;
		switch (index) {
		case 0:
			start = 0;
			end = 6;
			break;
		case 1:
			start = 6;
			end = 18;
			break;
		case 2:
			start = 18;
			end = 24;
			break;
		default:
			break;
		}
		for(var i=start;i<end;i++){
			var id = content[i].SID;//软件大类：1，lkj软件，4.asts软件
			var type = content[i].TYPE;//软件类型
			var softId = id+"_"+type;
			softIds.push(softId);
			str+='<div id='+softId+'><span class="spans">';
			str+=content[i].SYS_NAME;
			str+='</span><select class="signSelect">';
			str+=getVersionName(id,type);
		}
		//重置功能
		if(stateType == 0){
			setSelectState();
		}
	}
	return str;
}
//根据版本类型和软件类型查询下拉文件内容
function getVersionName(id,type){
	var str="";
	$.ajax({
		url : "../../../LKJSoftwareVersion/getVersionName",
		type : "post",
		data : {
			"sId" : id,
			"type" : type
		},
		dataType : "json",
		async:false,
		success : function(data) {
			if(data.flag==0){
				var content = data.content;
				str=ajaxOption(content);
			}
		},
		error : function() {
		}
	});
	str+='</select>';
	var str1 = "1,3,4,5,6";
	var str2 = "7,8,9,10,11,12,13";
	if(id==1&&str1.indexOf(type)>=0){
		str+="<select class='multipleSelect' multiple='multiple' > <optgroup label='I 端' class='group-1'><option value='1-1' selected='selected'>A机</option>"+
        "<option value='1-2' selected='selected' >B机</option> </optgroup><optgroup label='II端' class='group-2'>"+
        "<option value='2-1' selected='selected'>A机</option><option value='2-2' selected='selected'>B机</option> </optgroup></select>";
	}else if(id==1&&type==2){
		str+="<select class='multipleSelect' multiple='multiple'><option value='1' selected='selected'>I 端</option><option value='2' selected='selected'>II端</option></select>";
	}else if(id==1&&str2.indexOf(type)>=0){
		str+="<select class='multipleSelect' multiple='multiple' > <optgroup label='I 系' class='group-1'><option value='1-1' selected='selected'>A模</option>"+
        "<option value='1-2' selected='selected' >B模</option> </optgroup><optgroup label='II系' class='group-2'>"+
        "<option value='2-1' selected='selected'>A模</option><option value='2-2' selected='selected'>B模</option> </optgroup></select>";
	}else if(id==1&&(type==14)){
		str+="<select class='multipleSelect' multiple='multiple'><option value='1' selected='selected'>I 系</option><option value='2' selected='selected'>II系</option></select>";
	}else if(id==1&&type==16){
		str+="<select class='multipleSelect' multiple='multiple' >" +
				" <optgroup label='电源' class='group-1'><option value='1-1' selected='selected'>I 系</option><option value='1-2' selected='selected' >II系</option> </optgroup>" +
        "<optgroup label='数字' class='group-2'><option value='2-1' selected='selected'>I 系</option><option value='2-2' selected='selected'>II系</option></optgroup>" +
        "<optgroup label='模拟' class='group-3'><option value='3-1' selected='selected'>I 系</option><option value='3-2' selected='selected'>II系</option> </optgroup>" +
        "<optgroup label='通信' class='group-4'><option value='4-1' selected='selected'>I 系</option><option value='4-2' selected='selected'>II系</option> </optgroup>" +
        "<optgroup label='主控' class='group-5'><option value='5-1' selected='selected'>I 系</option><option value='5-2' selected='selected'>II系</option> </optgroup>" +
        "<optgroup label='检测' class='group-6'><option value='6-1' selected='selected'>I 系</option><option value='6-2' selected='selected'>II系</option> </optgroup>" +
        "<optgroup label='冷却' class='group-7'><option value='7-1' selected='selected'>I 系</option><option value='7-2' selected='selected'>II系</option> </optgroup>" +
        "<optgroup label='制动' class='group-8'><option value='8-1' selected='selected'>制动</option> </optgroup>" +
        "<optgroup label='隔离' class='group-9'><option value='9-1' selected='selected'>隔离</option> </optgroup>" +
        "</select>";
	}else if(id==1&&(type==15||type==17||type==19||type==21)){
		str+="<select class='multipleSelect' multiple='multiple'><option value='1' selected='selected'>A机</option><option value='2' selected='selected'>B机</option></select>";
	}else{
		str+="<div style='width:100px'></div>";
	}
	return str+='</div>';
}
//将查询到的下拉内容组合放到对应的位置
function ajaxOption(content){
	var str="<option value=''></option>";
	if(content!=""&&content!=null){
		for(var i=0;i<content.length;i++){
			var name = content[i].VERSION_NAME;
			var name1 = "";
			var name2 = "";//将文件名以逗号分隔加入版本号信息
		    var	nameTemp = name.split(".");
			if(nameTemp.length==2){
				name1=nameTemp[0];
				name2="."+nameTemp[1];
			}else{
				name1=nameTemp[0];
			}
			str+="<option value="+content[i].VERSION_ID+">"+name1+"_"+content[i].VERSION_NUMBER+name2+"("+content[i].VERSION_DETAIL+")"+"</option>";
		};
	}
	return str;
}

function loadPlatFormList() {
	$.ajax({
		url : "../../../testTask/queryPlatFormList",
		type : "POST",
		success : function(data) {
			var content = data.content;
			if (data.flag==0) {
				var sli = '';
				for (var i = 0; i < content.length; i++) {
					sli += '<option value="' + content[i].platformId + '">' + content[i].platformName
							+ '</option>';
				}
				var $li = $(sli);
				$('#platformId').append($li);
			}else{
				danger(data.errorInfo);
			}
		}
	});
}
var changeObjs = [];
//lkj换装跳转显示选择的版本信息
function showVersions(){
	changeObjs = [];
	//根据软件id获取当前选中的文件id和换装位置信息并进行封装
	if(softIds.length>0){
		for(var i=0;i<softIds.length;i++){
			var changeObj = {};
			var softId = softIds[i];
			var fileObj = $("#"+softId+" select.signSelect");
			var spanValue = $("#"+softId+" .spans").text();
			var fileId = "";
			if(fileObj.length>0){
				fileId = fileObj[0].value;
				changeObj.fileId = fileId;
				
				changeObj.versionType = '1';
			}
			if(fileId!=""){//如果选择要更换的文件则查询当前选中的换装位置
				var softChangePosition = "";
				var softChangeValue = "";
				var selectdOption = $("#"+softId+" select.multipleSelect");
				if(selectdOption.length>0){
					for(var j=0;j<selectdOption[0].length;j++){
						if(selectdOption[0][j].selected){
							softChangePosition+="1";
						}else{
							if(softChangeValue == ""){
								softChangeValue = selectdOption[0][j].value;
							}else{
								softChangeValue = softChangeValue+"||"+selectdOption[0][j].value;
							}
							softChangePosition+="0";
						}
					}
				}else{
					softChangePosition = "1";
				}
				changeObj.id = softId.split('_')[1]+"_"+softChangePosition;
				changeObj.position=softChangePosition;//换装位置可为空
				changeObj.selectId = softId;
				changeObj.softChangeValue = softChangeValue;
				changeObj.spanValue = spanValue;
				changeObjs.push(changeObj);
			}
		}
		
		var checkBool = false;//true:选择的软件没有选择系别，false：都选了系别
		for(var k = 0;k<changeObjs.length;k++){
			var position = changeObjs[k].position.split('');
			var count = 0;
			for(var n = 0;n<position.length;n++){
				count = count+Number(position[n]);
			}
			if(count == 0){
				checkBool = true;
			}
		}
		//添加校验，如果一个文件也没选中进行提示不发送请求
		if(changeObjs.length<=0 || checkBool){
			warning("请选择要换装的软件！");
			return;
		}
		//DIM放到最后，DIM放到最最后
		setDMI();
		lkjPlatFormId = $('#platformId').val();
		window.location.href='../html/LKJSoftwareChange.html';
		window.localStorage.setItem('changeObjs', JSON.stringify(changeObjs));
		window.localStorage.setItem('lkjPlatFormId', lkjPlatFormId);
	}
}
//DIM放到最后，DIM放到最最后
function setDMI(){
	var endDIM = -1;
	for(var i = 0;i<changeObjs.length;i++){
		if(changeObjs[i].spanValue =="DMI软件"
			||changeObjs[i].spanValue=="读卡软件"
				||changeObjs[i].spanValue =="按键软件"
					||changeObjs[i].spanValue =="资源文件"
						||changeObjs[i].spanValue =="字库"
							||changeObjs[i].spanValue =="语音"){
			endDIM++;
		}else{
			break;
		}
	}
	var newDIMDataArray = changeObjs.slice(0,endDIM+1);
	changeObjs = changeObjs.slice(endDIM+1);
	var endDIMCell = -1;
	for(var i = 0;i<newDIMDataArray.length;i++){
		if(newDIMDataArray[i].spanValue == "DMI软件"){
			endDIMCell++;
		}else{
			break;
		}
	}
	var newDIMDataArray1 = newDIMDataArray.slice(0,endDIMCell+1);
	var newDIMDataArray2 = newDIMDataArray.slice(endDIMCell+1);
	var lastDIMArray = newDIMDataArray2.concat(newDIMDataArray1);
	changeObjs = changeObjs.concat(lastDIMArray);
}
//根据历史记录显示数据
function setSelectState(){
	if(!changeObjs){
		return;
	}
	for(var i = 0;i<changeObjs.length;i++){
		$(".signSelect").find("option[value='"+changeObjs[i].fileId+"']").attr("selected",true);
		for(var j = 0;j<changeObjs[i].softChangeValue.split("||").length;j++){
			$("#"+changeObjs[i].selectId+" select.multipleSelect").find("option[value='"+changeObjs[i].softChangeValue.split("||")[j]+"']").attr("selected",false);
		}
	}
}
